<template>
  <h1>首页</h1>
  <div v-if="user==null">
    <router-link to="/reg">注册</router-link>
    <router-link to="/login">登录</router-link>
  </div>
  <div v-else>
    <h1>欢迎{{user.nickname}}回来</h1>
    <router-link to="/post"><el-button type="primary">发微博</el-button></router-link>
    <router-link to="/personal"><el-button type="primary">我的微博</el-button></router-link>
    <router-link to="/"><el-button type="primary" @click="logout()">退出</el-button></router-link>
  </div>
  <hr>
  <div v-for="weibo in weiboList">
    <router-link :to="'/detail?id='+weibo.id" style="text-decoration:none;color: #62b5ec" >
      <h3>{{weibo.nickname}}说:{{weibo.content}}</h3>
    </router-link>
    <img
        v-if="weibo.imgUrls!=null"
        :src="BASE_URL+url"
        width="100"
        v-for="url in weibo.imgUrls.split(',')"
    >
  </div>

</template>

<script setup>
    import {onMounted, ref} from "vue";
    import axios from "axios";

    const user =ref(localStorage.user?JSON.parse(localStorage.user):null);
    const logout=()=>{
      if(confirm("确定退出")){
        localStorage.clear();
        user.value=null;
      }
    }
    const  weiboList=ref([]);
//页面加载完成,自动执行的方法onMounted()
    onMounted(()=>{
      axios.get(BASE_URL+'/v1/weibo/list')
          .then((res)=>{
            if (res.data.code==2002){
              weiboList.value=res.data.data;
              console.log(weiboList.value);
            }
          })
    })



</script>
